package com.fausto.learnimprint.core.controller.impl

import com.fausto.learnimprint.core.controller.BlogModule
import com.fausto.learnimprint.core.controller.IndexModule
import com.fausto.learnimprint.core.model.dto.CommonResult
import com.fausto.learnimprint.core.service.IDiscussService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*

/**
 *
 *
 * 前端控制器
 *
 *
 * @author fausto
 * @since 2022-03-28
 */
@Api(tags = ["评论API"], basePath = "/discuss")
@RestController
@RequestMapping("/discuss")
class DiscussController : IndexModule, BlogModule {

    @Autowired
    lateinit var discussService: IDiscussService

    /**
     *      首页模块最新评论
     */
    @ApiOperation("首页获取最新评论")
    @GetMapping("/latest")
    override fun latestComments(): CommonResult<Any> {
        return try {
            CommonResult.ok("获取成功", data = discussService.latestComments())
        } catch (e: Exception) {
            CommonResult.fail("操作失败", data = e.message)
        }
    }

    /**
     *      用户在某一篇博文下发布评论
     */
    @ApiOperation("发布评论")
    @PreAuthorize("hasAuthority('USER')")
    @PostMapping("/{blogId}")
    fun saveDiscuss(
        @ApiParam(value = "评论的博文ID", example = "1")
        @PathVariable blogId: Int,
        @ApiParam(value = "评论内容", example = "评论测试")
        @RequestParam(required = true) discussBody: String
    ): CommonResult<Any> {
        return if (blogId < 1 || discussBody.isBlank()) {
            CommonResult.fail("参数错误")
        } else try {
            discussService.saveDiscuss(blogId, discussBody)
            CommonResult.ok("评论成功")
        } catch (e: Exception) {
            CommonResult.fail("操作失败", data = e.message)
        }
    }

    /**
     *      删除评论
     */
    @ApiOperation("删除评论")
    @PreAuthorize("hasAuthority('USER')")
    @DeleteMapping("/{discussId}")
    fun deleteDiscuss(
        @ApiParam(value = "评论ID", example = "1")
        @PathVariable discussId: Int
    ): CommonResult<Any> {
        return if (discussId < 1) {
            CommonResult.fail("参数错误")
        } else try {
            return when (discussService.deleteDiscuss(discussId)) {
                true -> {
                    CommonResult.ok("删除成功")
                }
                false -> {
                    CommonResult.fail("删除失败")
                }
            }
        } catch (e: RuntimeException) {
            CommonResult.fail("删除失败", data = e.message)
        }
    }


    /**
     *      分页查询博客下的所有评论
     */
    @ApiOperation("分页查询博文评论")
    @GetMapping("/{blogId}/{page}/{showCount}")
    override fun getDiscussFromBlog(
        @ApiParam(value = "博文ID", example = "1")
        @PathVariable blogId: Int,
        @ApiParam(value = "页面序号数", example = "1")
        @PathVariable page: Int,
        @ApiParam(value = "展示记录数", example = "5")
        @PathVariable showCount: Int
    ): CommonResult<Any> {
        return if (blogId < 1 || page < 1 || showCount < 1) {
            CommonResult.fail("参数错误")
        } else try {
            val discusses = discussService.findDiscussPage(blogId, page, showCount)
            CommonResult.ok("查询成功", data = discusses)
        } catch (e: Exception) {
            CommonResult.fail("操作失败", data = e.message)
        }
    }


    /**
     *      获取用户发布在所有博文下的所有评论
     */
    @ApiOperation(value = "获取用户的所有评论", notes = "获取用户发布的所有博文下的评论")
    @PreAuthorize("hasAuthority('USER')")
    @GetMapping("/userDiscuss")
    fun userDiscuss(): CommonResult<Any> {
        return try {
            CommonResult.ok("查询成功", data = discussService.findUserDiscuss())
        } catch (e: Exception) {
            CommonResult.fail("查询失败", data = e.message)
        }
    }

}